import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_reader/model/book/book_review_model.dart';
import 'package:flutter_reader/model/book_history_model/book_history_model.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

class DatabaseHelper {
  static DatabaseHelper _databaseHelper;
  static Database _database;

  String bookHisTable = 'book_his_table';
  String colId = 'id';
  String bookId = 'bookId';
  String bookName = 'bookName';
  String chaName = 'chaName';
  String chaId = 'chaId';
  String bookImage = 'bookImage';

  // String colBrand = 'brand';
  // String colType = 'type';
  // String colStart = 'start';

  DatabaseHelper._createInstance();

  factory DatabaseHelper() {
    if (_databaseHelper == null) {
      _databaseHelper = DatabaseHelper._createInstance();
    }
    return _databaseHelper;
  }

  Future<Database> get database async {
    if (_database == null) {
      _database = await initializeDatabase();
    }
    return _database;
  }

  Future<Database> initializeDatabase() async {
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + 'book_his.db';


    var bookHisDatabase =
    await openDatabase(path, version: 1, onCreate: _createDb);
    return bookHisDatabase;
  }

  void _createDb(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $bookHisTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $bookId TEXT,$bookName TEXT, $chaName TEXT, $chaId TEXT, $bookImage TEXT)');
  }

  //  读取数据
  Future<List<Map<String, dynamic>>> getBookMapList() async {
    Database db = await this.database;
    var result = await db.query(bookHisTable);
    return result;
  }

  //  增加数据
  Future<int> insertBook(BookHis his) async {
    Database db = await this.database;
    var result = await db.insert(bookHisTable, his.toMap());
    return result;
  }

  // 更新数据
  Future<int> updateBook(BookHis his) async {
    Database db = await this.database;
    debugPrint("现在是 第${his.chaName.toString()}  书是 ${his.bookId.toString()}");
    var result = await db.rawUpdate('UPDATE $bookHisTable SET chaName = ? WHERE bookId = ?', [his.chaName, his.bookId]);
    return result;
  }

  // 获取当前阅读到的章节


  //  刷新数据
  // Future<int> updateBook(BookHis his) async {
  //   Database db = await this.database;
  //   var result = await db.update(bookHisTable, his.toMap(),
  //       where: '$colId = ?', whereArgs: [his.id]);
  //   return result;
  // }

   //删除数据
  Future<int> deleteBook(int id) async {
    Database db = await this.database;
    int result =
    await db.rawDelete('DELETE FROM $bookHisTable WHERE $colId = $id');
    return result;
  }

  //  获取数据条数
  // Future<int> getCount() async {
  //   Database db = await this.database;
  //   List<Map<String, dynamic>> x =
  //   await db.rawQuery('SELECT COUNT (*) FROM $carsTable');
  //   int result = Sqflite.firstIntValue(x);
  //   return result;
  // }

  // 转化获得 List 类型数据
  Future<List<BookHis>> getBookList() async {
    var bookMapList = await getBookMapList();
    int count = bookMapList.length;

    List<BookHis> bookList = List<BookHis>();

    for (int i = 0; i < count; i++) {
      bookList.add(BookHis.fromMapObject(bookMapList[i]));
    }
    return bookList;
  }
}